package com.bt.lms.ui.action.school_education.stu_basic.familyMember;

import com.alibaba.fastjson.JSON;
import com.bt.lms.constants.StuStatusBizTypeConstant;
import com.bt.lms.entity.FamilyMemberInfo;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

/**
 * 家庭成员的删除功能
 */
public class DeleteVerifyAction extends VBox {

    private VBox box;

    private Alert alert;

    private List list;

    public DeleteVerifyAction(Alert alert, VBox vBox, List list) {
        this.alert = alert;
        this.box = vBox;
        this.list = list;
    }

    public void handle() {
        this.setPrefWidth(500);
        this.setPrefHeight(300);
        this.setAlignment(Pos.CENTER);
        HBox hBox = new HBox();
        hBox.setPadding(new Insets(20));
        hBox.setSpacing(50);
        Button saveBtn = new Button("确定");
        Button cancelBtn = new Button("取消");
        hBox.getChildren().addAll(saveBtn, cancelBtn);
        saveBtn.setOnAction(event -> {

            int size = list.size();
            if (size > 1) {
                ObservableList<Node> children = box.getChildren();
                children.remove(size);
                ButtonType buttonType = new ButtonType("");
                alert.getDialogPane().getButtonTypes().add(buttonType);
                alert.close();
                list.remove(size-1);

                FamilyMemberInfo familyMemberInfo = new FamilyMemberInfo();
                //根据网络协议组包i
                byte[] bytes = enCode(familyMemberInfo);
                //开网
                Socket socket = getNet();
                //发送消息
                boolean flag = sendMsg(socket, bytes);

            } else {
                alert.close();
                Alert alert1 = new Alert(Alert.AlertType.WARNING);
                alert1.setContentText("家庭成员只有一个，当前不能删除");
                alert1.show();
            }
        });
        cancelBtn.setOnAction(event -> {


            ButtonType buttonType = new ButtonType("");
            alert.getDialogPane().getButtonTypes().add(buttonType);
            alert.close();
        });

        this.getChildren().add(hBox);


    }

    /**
     * 发送消息
     *
     * @param socket       网
     * @param bytes 消息包
     * @return
     */
    private boolean sendMsg(Socket socket, byte[] bytes) {

        try {
            OutputStream out = socket.getOutputStream();
            out.write(bytes);
            out.flush();

            //接收响应消息
            InputStream in = socket.getInputStream();
            byte[] buf = new byte[1024];
            in.read(buf);

            RespMsg respMsg = RespMsg.toRespMsg(buf);
            String data = respMsg.getData();
            if (data != null){
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("删除成功");
                alert.show();
            }

            //关闭资源
            out.close();
            socket.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return true;
    }

    /**
     * 开网
     *
     * @return 网
     */
    private Socket getNet() {

        String ip = "127.0.0.1";
        int port = 8888;
        Socket socket = null;
        try {
            socket = new Socket(ip, port);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return socket;
    }

    /**
     * 组包
     *
     * @param familyMemberInfo
     * @return 消息包
     */
    private byte[] enCode(FamilyMemberInfo familyMemberInfo) {
        ReqMsg reqMsg = new ReqMsg();
        reqMsg.setBizType(StuStatusBizTypeConstant.FAMILY_MEMBER_BIZ_TYPE_DELETE);
        familyMemberInfo.setId(1L);
        String familyStr = JSON.toJSONString(familyMemberInfo);
        reqMsg.setData(familyStr);


        return reqMsg.toBytes();
    }
}
